﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DingTida.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DingTida.Controllers
{
    public class adminController : Controller
    {

        public EasyStarContext db;
        public adminController(EasyStarContext _db)
        {
            this.db = _db;
        }
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public IActionResult login(string username, string password)
        {
            if (username == string.Empty || password == string.Empty || username == "" || password == "")
            {

                return Ok(new { msg = "帐户密码不能为空" });

            }
            else
            {

                if (db.AppUser.Any(c => c.Username == username && c.Password == password))
                {
                    HttpContext.Session.SetString("username", username);
                    return Ok(new { msg = "ok" });
                }
                else
                {
                    return Ok(new { msg = "帐户或密码错误" });

                }

            }


        }

        public IActionResult manager()
        {


            return View();

        }

        public Item GetList(string  deptno ,Item it,List<EsDept> lst)
        {
            if(lst==null)
             lst = db.EsDept.ToList();
            if (deptno == null|| deptno == "")
            {
                it.id = "";
                it.title = "组织架构";

            }
            else {
                var im = lst.Where(c => c.FullCode == deptno).FirstOrDefault();
                it.id = im.FullCode;
                it.title = im.Fname;
            }
           
            it.children = new List<Item>();
            var llst = lst.Where(c => c.UpCode == it.id).ToList();
            foreach (var ii in llst)
            {
                var iot = new Item();
                iot.id = ii.FullCode;
                iot.title = ii.Fname;
                iot.children = new List<Item>();
                it.children.Add(iot);
                var cit=  GetList(ii.FullCode, iot, lst);
            
           
            }
            return it;


        }

        public IActionResult GetDeptList()
        {
            var item = new Item();


            var lst = GetList("",item,null);

            

            return Ok(
                   new {
                       code = 0,
                       msg = "",
                   
                       data = lst,



                   }


                );


        }

        [HttpGet]
        public IActionResult GetKmInfor()
        {

            var lst = db.AppCarSendInfo.Join(db.EmEmpData, c => c.StaffInUser, m => m.EmpNo, (c, m) => new
            {
                empno = c.StaffInUser,
                name = m.Fname,
                reason = c.Remark,
                departure = c.Departure,
                destination = c.Destination,
                usersnumber = c.Usersnumber,
                mobile = c.TelNo,
                usedate = c.NeedDateTime,
                id = c.Id,
                begin=c.Beginkm,
                end=c.Endkm,
                IsSelf=c.IsSelfCar,
               


            }).OrderByDescending(c => c.usedate).ToList().Take(100);

            //var llst = lst.OrderByDescending(c => c.usedate).ToList().Take(100);

            return Ok(new {code=0,count=lst.Count(), msg = "获取数据成功", data = lst });

            //code = 0,
            //        msg = "",
            //        count = lst.Count,
            //        data = lst,


        }
        [HttpGet]

        public IActionResult GetKmInforOfCar()
        {

            return View();



        }

    }

    public class Item
   {
        public string id { get; set; }
        public string title { get; set; }

        public List<Item> children { get; set; }

    }
}